package play.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import by.grsu.hdm.exceptions.EmptyTrackException;
import by.grsu.hdm.exceptions.IllegalInternalStateException;
import by.grsu.hdm.media.MediaPlayer;
import by.grsu.hdm.media.tracks.audio.AudioTracks;
import by.grsu.hdm.media.tracks.video.VideoTracks;



public class PlayTrackServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
			
			response.setCharacterEncoding("UTF-8");
			response.setContentType("text/html");
			
			PrintWriter out = response.getWriter();
			
			out.println("<html>");
	        out.println("<head>");
	        out.println("</head>");
	        out.println("<body>");
	        out.println("<h1 align = center>Media Player</h1>");
	        out.println("<br><br>");
	       
	        request.setCharacterEncoding("UTF-8");
	        String track = request.getParameter("track");
	        String field = request.getParameter("field");
	        MediaPlayer player = new MediaPlayer();
	        Map<Integer,String> tracks = new TreeMap<Integer,String>();
	        tracks.putAll(AudioTracks.getPlayList());
	        tracks.putAll(VideoTracks.getPlayList());
	      
	       
	        
	  if(track != null || field != null){ 
		  
		  if(track != null){
            	
            try {
				out.println("<h3 align=center>"+player.play(track)+"</h3>");
				out.print("<h3 align=center><a href="+"play.do"+">Play list</a></h3>");
			} catch (IllegalInternalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  }
		  
		  
		  if(field != null){
			  
			  
			try {
				if(0<Integer.parseInt(field) && Integer.parseInt(field)<=10){
				out.print("<h3 align=center>"+player.play(tracks.get(Integer.parseInt(field)))+"</h3>");
				out.print("<h3 align=center><a href="+"play.do"+">Play list</a></h3>");
				}else{
					throw new EmptyTrackException();
					
				}
				
			}catch(EmptyTrackException e)	{
				out.println("<table align=center>");
				out.println("<h3 align=center>Is no such a composition, enter id again</h3>");
				out.println("<h3 align=center><a  href="+"play.do"+">Go to Media Player</a></h3>");	
				out.println("</table>");
			}catch (NumberFormatException e) {
				out.println("<table align=center>");
				out.println("<h3 align=center>Is no such a composition, enter id again</h3>");
				out.println("<h3 align=center><a  href="+"play.do"+">Go to Media Player</a></h3>");	
				out.println("</table>");
			} catch (IllegalInternalStateException e) {
				e.printStackTrace();
			}
		  }
            

	  }else{
	      
		  out.println("<h3 align=center>List of the audio and video tracks</h3>");
          out.println("<h4 align=center>Select audio or video track and push to play</h4>");

		  out.print("<form name=form1 action="+"play.do "+ "method=POST>");
		     
		      out.print("<table align=center>");
		       out.print("<tr>");
		      	 out.print("<td align=left>");
		        out.print("<p align=center>Audio</p>");
		       
		        Map<Integer,String> audio = AudioTracks.getPlayList();
		        	        
		        for(int i=1;i<=audio.size();i++){
		        out.println(i+". ");
		        out.println("<input type=radio name=track value="+audio.get(i)+">"+audio.get(i)+"<br>");
		        }
		        out.print("</td>");	
		       	        
		        Map<Integer,String> video = VideoTracks.getPlayList();
		         
		         out.print("<td>");
		        out.print("<p align=center>Video</p>");
		        for(int i=6;i<=10;i++){
		        out.println(i+". ");
		        out.println("<input type=radio name=track value="+video.get(i)+">"+video.get(i)+"<br>");
		        }
		         out.print("</td>");
		      out.print("</tr>");  
		      
		      out.print("<tr>"); 
		         out.print("<td colspan=2 align=center>");
		         out.println("<input type=submit name=submit value=Play>");
		         out.print("</td>");
		         out.print("<td>");
		         out.print("</td>");
		      out.print("</tr>"); 
		      
		     out.print("</table>");
	  
	  		out.println("</form>");
	  		
	  		out.print("<table align=center>");
	  			out.print("<tr>");
	  			out.print("<td colspan=2 align=center>");
	  		out.println("<form name = form2 action="+"play.do "+"method=POST>");
	  					out.print("<h3>Enter id of track: </h3>");
	  					out.println("<input type=text name=field size=20>");
	  					
	  					out.print("<input type=submit name=submit value=Play>");
	  		out.println("</form>");
	  			out.print("</td>");
	  			out.print("<td>");
	  			out.print("</td>");
	  			
	  		out.print("</table>");
	  }
	        out.println("</body>");
	        out.println("</html>");
	  
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		doGet(request,response);
			
	}
}


